package com.csi.controller.employee;

import com.alibaba.fastjson2.JSON;
import com.csi.domain.Employee;
import com.csi.pojo.PageBean;
import com.csi.pojo.Result;
import com.csi.service.EmployeeService;
import com.csi.service.impl.EmployeeServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

@WebServlet("/employee/findByIdController")
public class FindByIdController extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/json;charset=utf-8");
        PrintWriter out = resp.getWriter();
        Result<Employee> result = new Result<>();
        Integer userId = null;
        try {
            userId = Integer.parseInt(req.getParameter("userId"));
        } catch (NumberFormatException e) {
            result.setCode(400);
            result.setMsg("参数转换异常");
            out.print(JSON.toJSONString(result));
            out.flush();
            out.close();
            return;
        }
        EmployeeService employeeService = new EmployeeServiceImpl();
        Employee employee = employeeService.findById(userId);
        if (employee != null) {
            result.setCode(200);
            result.setMsg("success");
            result.setDatas(employee);
        } else {
            result.setCode(400);
            result.setMsg("false");
        }
        out.print(JSON.toJSONString(result));
        out.flush();
        out.close();
    }


}
